package com.Glow.reggie.controller;

import com.Glow.reggie.common.R;
import com.Glow.reggie.entity.Employee;
import com.Glow.reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author Glow
 * @Date 2022-07-27 14:21:19
 * @Description EmployeeController 懒得打中文了，类似了
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param httpServletRequest
     * @param employee
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpServletRequest  httpServletRequest,@RequestBody Employee employee)
    {
        //1、对密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据前台发送的用户名是否存在于employee中
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee em = employeeService.getOne(lambdaQueryWrapper);
        //3、查看用户名是否存在，失败则返回
        if(em == null)
        {
            return R.error("failLogin");
        }
        //4、查看用户名对应的密码是否一致
        if (!em.getPassword().equals(password))
        {
            return R.error("FalsePassword");
        }
        //5、查看用户名对应的员工状态是否被禁用
        if(em.getStatus() == 0)
        {
            return R.error("AccessOutOfDate");
        }
        //6、都没问题，则返回登录成功并返回sessionID和登录成功结果
        httpServletRequest.getSession().setAttribute("employee", em.getId());

        return R.success(em);
    }

    /**
     * 员工退出系统
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request)
    {
        //清理session中保存的当前登陆员工的ID
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee)
    {
        log.info("新增员工的信息为：{}",employee.toString());
        //DefaultPassword 123456
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //获得当前登陆用户的ID
//        Long empID = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empID);
//        employee.setUpdateUser(empID);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page,int pageSize,String name)
    {
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
        employeeService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee)
    {
        log.info(employee.toString());
        Long id = Thread.currentThread().getId();
        log.info("线程ID为:{}",id);

        //加了自动填充，不需要了
//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功！");
    }
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id)
    {
        log.info("根据员工ID查询中");
        Employee employee = employeeService.getById(id);
        if(employee != null)
        {
            return R.success(employee);
        }
        else {
            return R.error("noThisIDMessage！");
        }
    }
}
